import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.awt.*;
import javax.imageio.ImageIO;


public class Enemy {
    private BufferedImage enemimag;
    private BufferedImage enemimag2;
    private Point point;
    private int health;
    private final int INITHEALTH = 50;
    private Ellipse2D bounds;
    private boolean right;
    
    public Enemy(Point point){
        try {
            enemimag = ImageIO.read(new File("enemy.png"));
            enemimag2 = ImageIO.read(new File("enemyRight.png"));
            //Thanks to http://metalslug.wikia.com/wiki/File:Sprite_gameboy_soldier.gif for the image!//
        } catch (IOException e) {
        	e.printStackTrace();
        }
        this.point = point;
        this.health = INITHEALTH;
        bounds = new Ellipse2D.Double(getX(), getY(), enemimag.getHeight(), enemimag.getWidth());
    }
    
    public void shoot(ArrayList<Bullet> bull, Point charpoint){
    	if(right){
    		bull.add(new Bullet(new Point(getX() + 15, getY() + 15), 
    				 new Point((int)charpoint.getX() + 20, 
    				(int)charpoint.getY() + 20), true));
    	}
    	else{
    		bull.add(new Bullet(new Point(getX() + 5, getY() + 15), 
    				 new Point((int)charpoint.getX() + 20, 
    				(int)charpoint.getY() + 20), true));
    	}
    }
    
    public BufferedImage horizontalFlip(BufferedImage img) {
		int w = img.getWidth();
		int h = img.getHeight();
		BufferedImage dimg = new BufferedImage(w, h, img.getType());
		Graphics2D g = dimg.createGraphics();
		g.drawImage(img, 0, 0, w, h, w, 0, 0, h, null);
		g.dispose();
		return dimg;
	}
    
    
    public BufferedImage getImage(){
        if(!right){
        	return enemimag;
        }
        else{
        	return enemimag2;
        }       
    }
    
    public void move(Point p){
        if(p.getX() < this.getX()){
        	right = false;
            point.x--;
        }else if(p.getX() > this.getX()){
        	right = true;
            point.x++;
        }
        if(isRight()){
        	bounds = new Ellipse2D.Double(getX(), getY(), getImage().getWidth()-10, getImage().getHeight());
        }
        else{
        	bounds = new Ellipse2D.Double(getX()+10, getY(), getImage().getWidth()-10, getImage().getHeight());
        }
    }
    
    public Point getPoint(){
    	return point;
    }

    public int getX(){
    	return point.x;
    }

    public int getY(){
    	return point.y;
    }
    
	public boolean isCollided(Point p){
		if(bounds.contains(p)){
			return true;
		}
		return false;
	}
    
    public Shape getBounds(){
    	return bounds;
    }
    public boolean isRight(){
		return right;
	}
    public void loseHealth(){
    	health -= 10;
    }
    public int getHealth(){
		return health;
	}
}

